package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.UserException;
import com.uzhie.exception.WarningException;
import com.uzhie.jt808.util.DateUtil;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.Warning;
import com.uzhie.web.module.WarningCollect;
import com.uzhie.web.service.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报警控制对象
 * Created by lilongfei on 2018/8/7.
 */
@RestController
@RequestMapping(value = "warning")
public class WarningController extends CommController {

    @Autowired
    private WarningService service;


    /**
     * 查询所有的报警分类,并回传未处理报警数量
     *
     * @param body userId 用户ID
     * @return
     */
    @Auth
    @RequestMapping(value = "queryLabel")
    public PayloadBean queryLabel(@RequestBody(required = false) JSONObject body) {
        try {
            List<JSONObject> result = service.queryAllLebal(body.getString("userId"));
            return buildResponse(result);
        } catch (IllegalArgumentException | UserException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 查询所有未处理报警
     *
     * @param body category 报警类型
     *             departIds 部门ID集合
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "queryListByTypeAndDepart")
    public PayloadBean queryListByTypeAndDepart(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        try {
            String category = body.getString("category");
            String userId = body.getString("userId");
            String vehicleNumber = body.getString("vehicleNumber");
            String driver = body.getString("driver");
            String startTime = body.getString("startTime");
            String endTime = body.getString("endTime");
            List<Warning> result = service.queryListByTypeAndDepart(category, userId, vehicleNumber, driver, startTime, endTime, page.getRowBounds());
            return buildResponseWithPage(result, page);
        } catch (IllegalArgumentException | UserException e) {
            return buildResponseWithPage(null, page, setError(0, e.getMessage()));
        }
    }


    /**
     * 查询今天所有未处理报警
     *
     * @param body category 报警类型
     *             departIds 部门ID集合
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "queryListByTypeAndDepartToday")
    public PayloadBean queryListByTypeAndDepartToday(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        try {
            String category = body.getString("category");
            String userId = body.getString("userId");
            String vehicleNumber = body.getString("vehicleNumber");
            String startTime = DateUtil.datetimeToString(new Date()) + " 00:00:00";
            String endTime = DateUtil.dateToString(new Date());
            List<Warning> result = service.queryListByTypeAndDepart(category, userId, vehicleNumber, null, startTime, endTime, page.getRowBounds());
            return buildResponseWithPage(result, page);
        } catch (IllegalArgumentException | UserException e) {
            return buildResponseWithPage(null, page, setError(0, e.getMessage()));
        }
    }


    /**
     * 报警处理,更新报警信息的状态
     *
     * @param body warnIds 报警对象
     *             status 修改的后的报警信息状态
     * @return
     */
    @Auth
    @RequestMapping(value = "updateWarnStatus")
    public PayloadBean updateWarnStatus(@RequestBody(required = false) JSONObject body) {
        try {
            List<Warning> warns = JSON.parseArray(body.getJSONArray("warnIds").toJSONString(), Warning.class);
            Integer status = body.getInteger("status");
            String remark = body.getString("remark");
            String handlerId = body.getString("handlerId");
            String handlerName = body.getString("handlerName");
            service.updateWarningStatus(warns, status, remark, handlerId, handlerName);
            return buildResponse(null);
        } catch (IllegalArgumentException | WarningException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 下达拍照指令
     *
     * @param body vehicleNumber 车载sim卡号
     *             channel 通道号
     *             takeOrder 连续拍照数
     *             resolution 分辨率
     * @return
     */
    @Auth
    @RequestMapping(value = "takePic")
    public PayloadBean takePic(@RequestBody(required = false) JSONObject body) {
        try {
            service.takePic(body.getString("vehicleNumber"), body.getInteger("channel"), body.getInteger("takeOrder"), body.getInteger("resolution"));
            return buildResponse(null);
        } catch (IllegalArgumentException | WarningException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 报警处理报表查询
     *
     * @return
     */
    @Auth
    @RequestMapping(value = "handlerReport")
    public PayloadBean handlerReport(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        List<Warning> result = null;
        try {
            result = service.handlerReport(
                    body.getString("userId"),
                    body.getString("simNumber"),
                    body.getString("vehicleNumber"),
                    body.getString("driver"),
                    body.getString("telephone"),
                    body.getString("handlerName"),
                    body.getString("beginTime"),
                    body.getString("endTime"),
                    body.getString("handBeginTime"),
                    body.getString("handEndTime"),
                    page
            );
            return buildResponseWithPage(result, page);
        } catch (IllegalArgumentException e) {
            return buildResponseWithPage(null, page, setError(0, e.getMessage()));
        }
    }


    /**
     * 根据用户管理权限查询车辆报警信息
     *
     * @return
     */
    @Auth
    @RequestMapping(value = "queryCollectListByUserPower")
    public PayloadBean queryCollectListByUserPower(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        try {
            String category = body.getString("category");
            String userId = body.getString("userId");
            String vehicleNumber = body.getString("vehicleNumber");
            String driver = body.getString("driver");
            String startTime = body.getString("startTime");
            String endTime = body.getString("endTime");
            List<WarningCollect> result = service.queryCollectByUserPowerAndCondition(userId, category, vehicleNumber, driver, startTime, endTime, page.getRowBounds());
            return buildResponseWithPage(result, page);
        } catch (IllegalArgumentException | UserException e) {
            return buildResponseWithPage(null, page, setError(0, e.getMessage()));
        }
    }

    /**
     * 处理汇总的报警信息
     *
     * @param body
     * @return
     */
    @Auth
    @RequestMapping(value = "updateWarnCollect")
    public PayloadBean updateWarnCollect(@RequestBody(required = false) JSONObject body) {
        String collect = body.getString("collect");
        String handlerId = body.getString("handlerId");
        String handlerName = body.getString("handlerName");
        String remark = body.getString("remark");
        Integer status = body.getInteger("status");
        List<WarningCollect> collects = JSONArray.parseArray(collect, WarningCollect.class);
        try {
            service.updateWarningCollect(collects, handlerId, handlerName, remark, status);
            return buildResponse(null);
        } catch (WarningException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 查询苏标 高级驾驶辅助报警
     *
     * @param body {
     *             "number" 车牌号
     *             "startTime" 上报开始时间
     *             "endTime" 上报结束时间
     *             }
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "queryJSDrivingAssistanceSystemWarn")
    public PayloadBean queryJSDrivingAssistanceSystemWarn(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        String vehicleNo = body.getString("number");
        String startTime = body.getString("startTime");
        String endTime = body.getString("endTime");
        List<Map<String, Object>> maps = service.queryJSDrivingAssistanceSystemWarn(vehicleNo, startTime, endTime, page);
        return buildResponseWithPage(maps, page);
    }

    /**
     * 查询苏标 驾驶员状态监测报警
     *
     * @param body {
     *             "number" 车牌号
     *             "startTime" 上报开始时间
     *             "endTime" 上报结束时间
     *             }
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "queryJSDriverStatusMonitorSystemWarn")
    public PayloadBean queryJSDriverStatusMonitorSystemWarn(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        String vehicleNo = body.getString("number");
        String startTime = body.getString("startTime");
        String endTime = body.getString("endTime");
        List<Map<String, Object>> maps = service.queryJSDriverStatusMonitorSystemWarn(vehicleNo, startTime, endTime, page);
        return buildResponseWithPage(maps, page);
    }

    /**
     * 查询苏标 胎压监测报警
     *
     * @param body {
     *             "number" 车牌号
     *             "startTime" 上报开始时间
     *             "endTime" 上报结束时间
     *             }
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "queryJSTirePressureMonitorSystemWarn")
    public PayloadBean queryJSTirePressureMonitorSystemWarn(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        String vehicleNo = body.getString("number");
        String startTime = body.getString("startTime");
        String endTime = body.getString("endTime");
        List<Map<String, Object>> maps = service.queryJSTirePressureMonitorSystemWarn(vehicleNo, startTime, endTime, page);
        return buildResponseWithPage(maps, page);
    }

    /**
     * 查询苏标 盲区监测报警
     *
     * @param body {
     *             "number" 车牌号
     *             "startTime" 上报开始时间
     *             "endTime" 上报结束时间
     *             }
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "queryJSBlindAreaMonitorSystemWarn")
    public PayloadBean queryJSBlindAreaMonitorSystemWarn(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        String vehicleNo = body.getString("number");
        String startTime = body.getString("startTime");
        String endTime = body.getString("endTime");
        List<Map<String, Object>> maps = service.queryJSBlindAreaMonitorSystemWarn(vehicleNo, startTime, endTime, page);
        return buildResponseWithPage(maps, page);
    }

    @Auth
    @RequestMapping(value = "updateJSDrivingAssistanceSystemWarn")
    public PayloadBean updateJSDrivingAssistanceSystemWarn(@RequestBody JSONObject body) {
        String id = body.getString("id");
        String remark = body.getString("remark");
        Integer status = body.getInteger("status");
        if (service.updateJSDriverStatusMonitorSystemWarn(id, remark, status)) {
            return buildResponse(null);
        } else {
            return buildResponse(null, "操作失败");
        }
    }

    @Auth
    @RequestMapping(value = "updateJSDriverStatusMonitorSystemWarn")
    public PayloadBean updateJSDriverStatusMonitorSystemWarn(@RequestBody JSONObject body) {
        String id = body.getString("id");
        String remark = body.getString("remark");
        Integer status = body.getInteger("status");
        if (service.updateJSDriverStatusMonitorSystemWarn(id, remark, status)) {
            return buildResponse(null);
        } else {
            return buildResponse(null, "操作失败");
        }
    }

    @Auth
    @RequestMapping(value = "updateJSTirePressureMonitorSystemWarn")
    public PayloadBean updateJSTirePressureMonitorSystemWarn(@RequestBody JSONObject body) {
        String id = body.getString("id");
        String remark = body.getString("remark");
        Integer status = body.getInteger("status");
        if (service.updateJSDriverStatusMonitorSystemWarn(id, remark, status)) {
            return buildResponse(null);
        } else {
            return buildResponse(null, "操作失败");
        }
    }

    @Auth
    @RequestMapping(value = "updateJSBlindAreaMonitorSystemWarn")
    public PayloadBean updateJSBlindAreaMonitorSystemWarn(@RequestBody JSONObject body) {
        String id = body.getString("id");
        String remark = body.getString("remark");
        Integer status = body.getInteger("status");
        if (service.updateJSDriverStatusMonitorSystemWarn(id, remark, status)) {
            return buildResponse(null);
        } else {
            return buildResponse(null, "操作失败");
        }
    }

}
